﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Unmanned_MSUI.Areas.Admin.Filters
{
    /// <summary>
    /// 我的自定义筛选器
    /// </summary>
    public class MyFilterAttribute : Attribute, IActionFilter
    {
        public void OnActionExecuting(ActionExecutingContext filterContext)
        {
            //获取Session中存在的UserId
            var userId = filterContext.HttpContext.Session.GetString("StaffId");

            var userId2 = filterContext.HttpContext.Request.Cookies["StaffId"];
            //判断userId是否为空
            if (userId2 == null)
            {
                //var result = filterContext.Result;
                //为空则设置返回的结果为登录页面
                filterContext.Result = new RedirectResult("/Admin/Account/LoginView");
            }
        }

        /// <summary>
        /// 在执行操作之后
        /// </summary>
        /// <param name="filterContext"></param>
        public void OnActionExecuted(ActionExecutedContext filterContext)
        {

        }

    }
}
